<template>
  <el-dialog
    :title="dialogType ==='dialog_add'? text_add : text_update"
    :close-on-click-modal="isDebug"
    :visible.sync="dialogVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="130px" label-position="right">
    <el-form-item label="(老师)id" prop="teacherId">
      <el-input v-model="dataForm.teacherId" placeholder="(老师)id"></el-input>
    </el-form-item>
    <el-form-item label="(直播室房间)ID" prop="roomId">
      <el-input v-model="dataForm.roomId" placeholder="(直播室房间)ID"></el-input>
    </el-form-item>
    <el-form-item label="长江银/现货银/白金铜" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="长江银/现货银/白金铜"></el-input>
    </el-form-item>
    <el-form-item label="开仓时间" prop="kcTime">
      <el-input v-model="dataForm.kcTime" placeholder="开仓时间"></el-input>
    </el-form-item>
    <el-form-item label="开仓价" prop="kcPrice">
      <el-input v-model="dataForm.kcPrice" placeholder="开仓价"></el-input>
    </el-form-item>
    <el-form-item label="止损价" prop="zsPrice">
      <el-input v-model="dataForm.zsPrice" placeholder="止损价"></el-input>
    </el-form-item>
    <el-form-item label="止盈价" prop="zyPrice">
      <el-input v-model="dataForm.zyPrice" placeholder="止盈价"></el-input>
    </el-form-item>
    <el-form-item label="仓位数" prop="cwCount">
      <el-input v-model="dataForm.cwCount" placeholder="仓位数"></el-input>
    </el-form-item>
    <el-form-item label="平仓时间" prop="pcTime">
      <el-input v-model="dataForm.pcTime" placeholder="平仓时间"></el-input>
    </el-form-item>
    <el-form-item label="开仓策略" prop="kcPolicy">
      <el-input v-model="dataForm.kcPolicy" placeholder="开仓策略"></el-input>
    </el-form-item>
    <el-form-item label="平仓价" prop="pcPrice">
      <el-input v-model="dataForm.pcPrice" placeholder="平仓价"></el-input>
    </el-form-item>
    <el-form-item label="备注(老师对当前喊单的自我评价及风险评估)" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注(老师对当前喊单的自我评价及风险评估)"></el-input>
    </el-form-item>
    <el-form-item label="0表示已经平仓,1表示吗买入或者卖出" prop="type">
      <el-input v-model="dataForm.type" placeholder="0表示已经平仓,1表示吗买入或者卖出"></el-input>
    </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="timestamp"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">{{text_cancel}}</el-button>
      <el-button type="primary" @click="dialogType ==='dialog_add'?requestAdd():requestUpdate()">{{text_confirm}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        dialogType: 'dialog_add', // 弹窗类型 1添加 2编辑,
        dataForm: {
          id: 0,
          teacherId: '',
          roomId: '',
          productName: '',
          kcTime: '',
          kcPrice: '',
          zsPrice: '',
          zyPrice: '',
          cwCount: '',
          pcTime: '',
          kcPolicy: '',
          pcPrice: '',
          remark: '',
          type: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          teacherId: [{ validator: this.checkText, trigger: 'change' }],
          roomId: [{ validator: this.checkText, trigger: 'change' }],
          productName: [{ validator: this.checkText, trigger: 'change' }],
          kcTime: [{ validator: this.checkText, trigger: 'change' }],
          kcPrice: [{ validator: this.checkText, trigger: 'change' }],
          zsPrice: [{ validator: this.checkText, trigger: 'change' }],
          zyPrice: [{ validator: this.checkText, trigger: 'change' }],
          cwCount: [{ validator: this.checkText, trigger: 'change' }],
          pcTime: [{ validator: this.checkText, trigger: 'change' }],
          kcPolicy: [{ validator: this.checkText, trigger: 'change' }],
          pcPrice: [{ validator: this.checkText, trigger: 'change' }],
          remark: [{ validator: this.checkText, trigger: 'change' }],
          type: [{ validator: this.checkText, trigger: 'change' }],
          createTime: [{ validator: this.checkText, trigger: 'change' }],
          updateTime: [{ validator: this.checkText, trigger: 'change' }]
        }
      }
    },
    props: {
      rowData: {
        type: Object,
        default: null
      }
    },
    methods: {
      initDialog (dialogType) {
        this.dialogVisible = true
        this.dialogType = dialogType
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (dialogType === 'dialog_update') {
            this.requestInfo()
          }
        })
      },
      /** 请求信息(表单回显) */
      requestInfo () {
        this.$httpRequest({
          url: `/api/teacherhandan/info/${this.rowData.id}`,
          method: 'get'
        }).then((data) => {
          this.dataForm.teacherId = data.teacherId
          this.dataForm.roomId = data.roomId
          this.dataForm.productName = data.productName
          this.dataForm.kcTime = data.kcTime
          this.dataForm.kcPrice = data.kcPrice
          this.dataForm.zsPrice = data.zsPrice
          this.dataForm.zyPrice = data.zyPrice
          this.dataForm.cwCount = data.cwCount
          this.dataForm.pcTime = data.pcTime
          this.dataForm.kcPolicy = data.kcPolicy
          this.dataForm.pcPrice = data.pcPrice
          this.dataForm.remark = data.remark
          this.dataForm.type = data.type
          this.dataForm.createTime = data.createTime
          this.dataForm.updateTime = data.updateTime
        }, error => {
          this.showErrorToast(error.msg)
        })
      },
       /** 请求添加 */
      requestAdd () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/teacherhandan/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'teacherId': this.dataForm.teacherId,
                'roomId': this.dataForm.roomId,
                'productName': this.dataForm.productName,
                'kcTime': this.dataForm.kcTime,
                'kcPrice': this.dataForm.kcPrice,
                'zsPrice': this.dataForm.zsPrice,
                'zyPrice': this.dataForm.zyPrice,
                'cwCount': this.dataForm.cwCount,
                'pcTime': this.dataForm.pcTime,
                'kcPolicy': this.dataForm.kcPolicy,
                'pcPrice': this.dataForm.pcPrice,
                'remark': this.dataForm.remark,
                'type': this.dataForm.type,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      },
      /** 请求更新 */
      requestUpdate () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/teacherhandan/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'teacherId': this.dataForm.teacherId,
                'roomId': this.dataForm.roomId,
                'productName': this.dataForm.productName,
                'kcTime': this.dataForm.kcTime,
                'kcPrice': this.dataForm.kcPrice,
                'zsPrice': this.dataForm.zsPrice,
                'zyPrice': this.dataForm.zyPrice,
                'cwCount': this.dataForm.cwCount,
                'pcTime': this.dataForm.pcTime,
                'kcPolicy': this.dataForm.kcPolicy,
                'pcPrice': this.dataForm.pcPrice,
                'remark': this.dataForm.remark,
                'type': this.dataForm.type,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      }
    }
  }
</script>
